﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using KeZhou.Models.Instances;
using KeZhou.Views;
using System;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.Diagnostics;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;

namespace KeZhou.ViewModels
{
    internal partial class LoginViewModel: ObservableObject
    {

        private static LoginViewModel instance;

        public static LoginViewModel Instance
        {
            get
            {
                return instance ?? (instance = new LoginViewModel());
            }
        }

        /**
         * 登录
         */
        [ObservableProperty]
        private string account;

        [ObservableProperty]
        private string password;

        //登录按钮 
        //TODO
        public int loginSure()
        {
            //6: 手机号未注册
            //7：密码错误
            //10: 服务器问题登陆失败
            //13: 登陆成功,建立PlanOnBuilding的Instance
            string checkAccount = Account;
            string checkPassword = Password;
            string nickName = "defaultNickName";//昵称未开发
            int result = loginCheck(checkAccount, checkPassword);
            if ((result == 13) && Application.Current.MainWindow is MainWindow mainWindow)
            {
                int accountId = getAccountId(checkAccount);
                PlanOnBuilding.Instance = new PlanOnBuilding(accountId,checkAccount, checkPassword, nickName);
                mainWindow.pageJump();
            }

            /*//AAA
            //删除
            if (Application.Current.MainWindow is MainWindow mainWindow1)
            {
                int accountId = getAccountId(checkAccount);
                PlanOnBuilding.Instance = new PlanOnBuilding(accountId, checkAccount, checkPassword, nickName);
                mainWindow1.pageJump();
            }
            //AAA*/

            return result;
        }

        //登陆检查
        private int loginCheck(string phoneNum, string password)
        {
            using (HttpClient client = new HttpClient())
            {
                // 构建请求参数
                var requestData = new Dictionary<string, string>
                {
                    { "phoneNum", phoneNum },
                    { "password", password }
                };

                // 构建请求地址
                string requestUrl = "http://www.yym-free.com:4514/kezhou/cloud/signInCheck";

                // 发送POST请求
                HttpResponseMessage response = client.PostAsync(requestUrl, new FormUrlEncodedContent(requestData)).Result;

                // 检查响应是否成功
                if (response.IsSuccessStatusCode)
                {
                    // 解析并返回响应内容，这里假设返回的是整数
                    int result = int.Parse(response.Content.ReadAsStringAsync().Result);
                    return result;
                }
                else
                {
                    // 处理请求失败的情况，可以根据需要进行适当的处理
                    Console.WriteLine("HTTP POST请求失败：" + response.StatusCode);
                    return -1; // 返回一个适当的错误码
                }
            }
        }

        //通过账号获取id
        private int getAccountId(string phoneNum)
        {
            using (HttpClient client = new HttpClient())
            {
                // 构建请求参数
                var requestData = new Dictionary<string, string>
                {
                    { "phoneNum", phoneNum },
                };

                // 构建请求地址
                string requestUrl = "http://www.yym-free.com:4514/kezhou/cloud/getAccountId";

                // 发送POST请求
                HttpResponseMessage response = client.PostAsync(requestUrl, new FormUrlEncodedContent(requestData)).Result;

                // 检查响应是否成功
                if (response.IsSuccessStatusCode)
                {
                    // 解析并返回响应内容，这里假设返回的是整数
                    int result = int.Parse(response.Content.ReadAsStringAsync().Result);
                    return result;
                }
                else
                {
                    // 处理请求失败的情况，可以根据需要进行适当的处理
                    Debug.WriteLine("HTTP POST请求失败：" + response.StatusCode);
                    //AAA
                    //原本为-1
                    return -1; // 返回一个适当的错误码
                }
            }
        }

    }
}
